package com.example.projecttest;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.example.projecttest.impl.SelectService;
import com.example.projecttest.model.SelectModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class RetrofitActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit);

        initView();
    }

    private void initView() {
        mTextView = findViewById(R.id.mTextView);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(SelectService.BaseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        SelectService service = retrofit.create(SelectService.class);
        //Call<ResponseBody> call = service.getSelectData();

        //同步请求
        //call.execute();

        //异步请求
        /*call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                        String json = response.body().string();
                        mTextView.setText(json);
                    } catch (IOException e) {
                        mTextView.setText(e.toString());
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {

            }
        });*/

        Call<SelectModel> call = service.getSelectData();
        call.enqueue(new Callback<SelectModel>() {
            @Override
            public void onResponse(Call<SelectModel> call, Response<SelectModel> response) {
                if (response.isSuccessful()) {
                    SelectModel model = response.body();
                    mTextView.append(model.getData().getId());
                }
            }

            @Override
            public void onFailure(Call<SelectModel> call, Throwable t) {

            }
        });
    }
}
